package dale.web.core.log;

import java.util.*;
import javax.servlet.http.*;
import org.apache.commons.logging.Log;

public class HttpLogger {

    private HttpServletRequest request;
    private HttpServletResponse response;
	
    public HttpLogger(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    public void logBefore(Log log) {
        String requestStr = logRequest();
        String headerStr = logRequestHeaders();
        String parameterStr = logRequestParameters();
        String format = "#################################### Log Before ############################################\n";
        format += "########## Request Detail ###############\n%s";
        format += "########## Header List ##########\n%s";
        format += "########## Parameter List ##########\n%s";
        String formattedStr = String.format(format, requestStr, headerStr, parameterStr);
        log.debug(formattedStr);
    }
    
    public void logAfter(Log log) {
        String attributeStr = logRequestAttributes();
        String sessionStr = logSessionAttributes();
        String format = "#################################### Log After ############################################\n";
        format += "########## Attribute List ##########\n%s";
        format += "########## Session Attribute List ##########\n%s";
        String formattedStr = String.format(format, attributeStr, sessionStr);
        log.debug(formattedStr);
    }

    public String logRequest() {
        StringBuffer buf = new StringBuffer();
        String URL = request.getRequestURL().toString();
        String method = request.getMethod();
        buf.append((new StringBuilder("- URL : ")).append(URL).append("\n").toString());
        buf.append((new StringBuilder("- method : ")).append(method).append("\n").toString());
        return buf.toString();
    }

    public String logRequestHeaders() {
        StringBuffer buf = new StringBuffer();
        String headerName;
        String headerValue;
        for(Enumeration enu = request.getHeaderNames(); enu.hasMoreElements(); buf.append((new StringBuilder("- ")).append(headerName).append(" : ").append(headerValue).append("\n").toString()))
        {
            headerName = (String)enu.nextElement();
            headerValue = request.getHeader(headerName);
        }

        return buf.toString();
    }

    public String logRequestParameters() {
        StringBuffer buf = new StringBuffer();
        SortedMap sMap = Collections.synchronizedSortedMap(new TreeMap(request.getParameterMap()));
        synchronized(sMap)
        {
            for(Iterator iterator = sMap.keySet().iterator(); iterator.hasNext();)
            {
                String key = (String)iterator.next();
                String values[] = (String[])sMap.get(key);
                String as[];
                int j = (as = values).length;
                for(int i = 0; i < j; i++)
                {
                    String value = as[i];
                    buf.append((new StringBuilder(String.valueOf(key))).append(" : ").append(value).append("\n").toString());
                }

            }

        }
        return buf.toString();
    }

    public String logRequestAttributes() {
        StringBuilder sb = new StringBuilder();
        String name;
        Object value;
        for(Enumeration enu = request.getAttributeNames(); enu.hasMoreElements(); sb.append((new StringBuilder("- ")).append(name).append(" : ").append(value).append("\n").toString()))
        {
            name = (String)enu.nextElement();
            value = request.getAttribute(name);
        }

        return sb.toString();
    }

    public String logSessionAttributes() {
        StringBuilder sb = new StringBuilder();
        HttpSession session = request.getSession(false);
        if(session != null)
        {
            String name;
            Object value;
            for(Enumeration enu = session.getAttributeNames(); enu.hasMoreElements(); sb.append((new StringBuilder("- ")).append(name).append(" : ").append(value).append("\n").toString()))
            {
                name = (String)enu.nextElement();
                value = session.getAttribute(name);
            }

        }
        return sb.toString();
    }

}
